﻿using RuYiAdmin.Net.Common.Global;
using RuYiAdmin.Net.Entity.BusinessDTO.SystemManagement;
using RuYiAdmin.Net.Entity.BusinessEntity.SystemManagement;
using RuYiAdmin.Net.Entity.CoreEntity;
using RuYiAdmin.Net.Entity.CoreEnum;
using RuYiAdmin.Net.Repository.BusinessRepository.SystemManagement.Language;
using RuYiAdmin.Net.Repository.BusinessRepository.SystemManagement.Menu;
using RuYiAdmin.Net.Repository.BusinessRepository.SystemManagement.MenuLanguage;
using RuYiAdmin.Net.Service.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace RuYiAdmin.Net.Service.BusinessService.SystemManagement.Menu
{
    /// <summary>
    /// 菜单业务层实现
    /// </summary>
    class MenuService : BaseService<SysMenu>, IMenuService
    {
        /// <summary>
        /// 菜单仓储实例
        /// </summary>
        private readonly IMenuRepository menuRepository;

        /// <summary>
        /// 菜单多语仓储实例
        /// </summary>
        private readonly IMenuLanguageRepository menuLanguageRepository;

        /// <summary>
        /// 多语仓储实例
        /// </summary>
        private readonly ILanguageRepository languageRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="menuRepository"></param>
        /// <param name="menuLanguageRepository"></param>
        /// <param name="languageRepository"></param>
        public MenuService(IMenuRepository menuRepository, IMenuLanguageRepository menuLanguageRepository,
            ILanguageRepository languageRepository) : base(menuRepository)
        {
            this.menuRepository = menuRepository;
            this.menuLanguageRepository = menuLanguageRepository;
            this.languageRepository = languageRepository;
        }

        #region 公有方法

        /// <summary>
        /// 获取菜单树
        /// </summary>
        /// <returns>ActionResult</returns>
        public QueryResult<SysMenuDTO> GetMenuTreeNodes()
        {
            var sqlKey = "sqls:sql:query_sysmenu";

            var strSQL = GlobalContext.Configuration.GetSection(sqlKey).Value;

            int totalCount = 0;

            var rootList = this.menuRepository.
                SqlQuery<SysMenuDTO>(new QueryCondition(), ref totalCount, strSQL).ToList();

            this.GetNodeChildren(rootList, sqlKey);

            var queryResult = new QueryResult<SysMenuDTO>();

            queryResult.HttpStatusCode = HttpStatusCode.OK;
            queryResult.Message = new String("OK");
            queryResult.List = new List<SysMenuDTO>();
            queryResult.List.AddRange(rootList);

            return queryResult;
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 递归树
        /// </summary>
        /// <param name="rootList">根节点</param>
        /// <param name="sqlKey">SQL键值</param>
        private void GetNodeChildren(List<SysMenuDTO> rootList, String sqlKey)
        {
            foreach (var root in rootList)
            {
                var queryCondition = new QueryCondition();
                queryCondition.QueryItems = new List<QueryItem>();
                queryCondition.QueryItems.Add(new QueryItem()
                {
                    Field = "ParentId",
                    DataType = DataType.Guid,
                    QueryMethod = QueryMethod.Equal,
                    Value = root.Id
                });

                var strSQL = GlobalContext.Configuration.GetSection(sqlKey).Value.Replace("AND isnull(ParentId)=1", "");

                int totalCount = 0;

                var list = this.menuRepository.SqlQuery<SysMenuDTO>(queryCondition, ref totalCount, strSQL);

                if (list.Count > 0)
                {
                    var languages = this.languageRepository.GetList();
                    var lanEn = languages.Where(t => t.LanguageName.Equals("en-US")).FirstOrDefault();
                    var lanRu = languages.Where(t => t.LanguageName.Equals("ru-RU")).FirstOrDefault();

                    foreach (var item in list)
                    {
                        var enMenu = this.menuLanguageRepository.GetList().
                            Where(t => t.MenuId.Equals(item.Id) && t.LanguageId.Equals(lanEn.Id) && t.IsDel.Equals(0)).FirstOrDefault();
                        var ruMenu = this.menuLanguageRepository.GetList().
                            Where(t => t.MenuId.Equals(item.Id) && t.LanguageId.Equals(lanRu.Id) && t.IsDel.Equals(0)).FirstOrDefault();

                        if (enMenu != null)
                        {
                            item.MenuNameEn = enMenu.MenuName;
                        }

                        if (ruMenu != null)
                        {
                            item.MenuNameRu = ruMenu.MenuName;
                        }
                    }

                    root.Children = new List<SysMenuDTO>();
                    root.Children.AddRange(list.OrderBy(t => t.Order).ToList());

                    this.GetNodeChildren(root.Children, sqlKey);
                }
            }
        }

        #endregion
    }
}
